package com.gugee.ins.data.monitor.listener;

import com.gugee.ins.data.common.consumer.AbstractGugeeConsumerHandler;
import com.gugee.ins.data.common.msg.EventMsg;
import com.gugee.ins.data.common.msg.InsBloggerMessage;
import com.gugee.ins.data.monitor.config.InsMonitorConfig;
import com.xiguaji.boot.toolkit.elasticsearch.RestClientHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.support.Acknowledgment;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description: 直接插入ES中，
 * User: luolifeng
 * Date: 2021-01-12 11:08
 */
@Component
@Slf4j
public class InsMonitorEventListener {

    @Autowired
    InsMonitorConfig insMonitorConfig;
    InsMonitorEventHandler insMonitorEventHandler = new InsMonitorEventHandler();

    /**
     * 完成监控事件的消费
     * @param messages
     * @param ack
     */
    @KafkaListener(id="${kafka.consumer.group.insMonitor.id}",topics = {"${kafka.topics.insMonitor.online}"},containerFactory = "factoryInsMonitorAck")
    public void bloggerMainCollect(List<EventMsg> messages, Acknowledgment ack) {

        insMonitorEventHandler.handle("InsMonitorEventListener",messages,120,ack);

    }

    class InsMonitorEventHandler extends AbstractGugeeConsumerHandler<EventMsg> {

        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMM");

        @Override
        public void dealMsg(List<EventMsg> messages){
            String nowMonth = simpleDateFormat.format(new Date());
            String esIndex=insMonitorConfig.getInsMonitorEventIndexPre()+"_"+nowMonth;
            RestClientHelper.add(esIndex,messages);
        }
    }

}
